package com.zmx.patientend.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;
import com.zmx.patientend.MyApplication;
import com.zmx.patientend.R;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.activity.AboutUsActivity;
import com.zmx.patientend.view.activity.ArticleCollectsActivity;
import com.zmx.patientend.view.activity.CollectionDoctorActivity;
import com.zmx.patientend.view.activity.ExpertConsultationActivity;
import com.zmx.patientend.view.activity.FeedbackActivity;
import com.zmx.patientend.view.activity.LoginActivity;
import com.zmx.patientend.view.activity.MeBangleActivity;
import com.zmx.patientend.view.activity.MePhysicianActivity;
import com.zmx.patientend.view.activity.MedicationDiaryActivity;
import com.zmx.patientend.view.activity.MessageActivity;
import com.zmx.patientend.view.activity.MyBalanceActivity;
import com.zmx.patientend.view.activity.MyOrderActivity;
import com.zmx.patientend.view.activity.MyPrescriptionActivity;
import com.zmx.patientend.view.activity.OrderManagementActivity;
import com.zmx.patientend.view.activity.PatientMessageActivity;
import com.zmx.patientend.view.activity.PersonalDataActivity;
import com.zmx.patientend.view.activity.SetUpActivity;
import com.zmx.patientend.view.util.MySharedPreferences;

import de.hdodenhof.circleimageview.CircleImageView;


public class MineFragment extends SimpleImmersionFragment implements View.OnClickListener {

    private RelativeLayout relative1, relative2, relative3, relative4;
    private RelativeLayout my_order1, my_order2, my_order3, my_order5;
    private RelativeLayout relativeLayout1, relativeLayout2, relativeLayout3, relativeLayout4, relativeLayout5, relativeLayout6, relativeLayout7, relativeLayout8;
    private TextView login_registered, phone;

    private CircleImageView image_head;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);

        phone = view.findViewById(R.id.phone);
        login_registered = view.findViewById(R.id.login_registered);
        login_registered.setOnClickListener(this);
        image_head = view.findViewById(R.id.image_head);
        relative1 = view.findViewById(R.id.relative1);
        relative1.setOnClickListener(this);
        relative2 = view.findViewById(R.id.relative2);
        relative2.setOnClickListener(this);
        relative3 = view.findViewById(R.id.relative3);
        relative3.setOnClickListener(this);
        relative4 = view.findViewById(R.id.relative4);
        relative4.setOnClickListener(this);

        my_order1 = view.findViewById(R.id.my_order1);
        my_order1.setOnClickListener(this);
        my_order2 = view.findViewById(R.id.my_order2);
        my_order2.setOnClickListener(this);
        my_order3 = view.findViewById(R.id.my_order3);
        my_order3.setOnClickListener(this);
        my_order5 = view.findViewById(R.id.my_order5);
        my_order5.setOnClickListener(this);

        relativeLayout1 = view.findViewById(R.id.relativeLayout1);
        relativeLayout1.setOnClickListener(this);
        relativeLayout2 = view.findViewById(R.id.relativeLayout2);
        relativeLayout2.setOnClickListener(this);
        relativeLayout3 = view.findViewById(R.id.relativeLayout3);
        relativeLayout3.setOnClickListener(this);
        relativeLayout4 = view.findViewById(R.id.relativeLayout4);
        relativeLayout4.setOnClickListener(this);
        relativeLayout5 = view.findViewById(R.id.relativeLayout5);
        relativeLayout5.setOnClickListener(this);
        relativeLayout6 = view.findViewById(R.id.relativeLayout6);
        relativeLayout6.setOnClickListener(this);
        relativeLayout7 = view.findViewById(R.id.relativeLayout7);
        relativeLayout7.setOnClickListener(this);
        relativeLayout8 = view.findViewById(R.id.relativeLayout8);
        relativeLayout8.setOnClickListener(this);
        return view;
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this).titleBarMarginTop(R.id.toolbar).statusBarDarkFont(true).init();
        getUserMessage();
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent();

        switch (v.getId()) {

            case R.id.login_registered:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                }

                break;

            case R.id.relative1:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), MePhysicianActivity.class);
                    startActivity(intent);
                }

                break;

            case R.id.relative2:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.putExtra("logo", 1);
                    intent.setClass(MineFragment.this.getContext(), PatientMessageActivity.class);
                    startActivity(intent);

                }

                break;

            case R.id.relative3:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), CollectionDoctorActivity.class);
                    startActivity(intent);
                }
                break;

            case R.id.relative4:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), ExpertConsultationActivity.class);
                    startActivity(intent);

                }
                break;

            case R.id.my_order1:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), OrderManagementActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("logo", "1");
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
                break;
            case R.id.my_order2:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), OrderManagementActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("logo", "2");
                    intent.putExtras(bundle);
                    startActivity(intent);

                }
                break;
            case R.id.my_order3:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), OrderManagementActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("logo", "3");
                    intent.putExtras(bundle);
                    startActivity(intent);

                }
                break;

            case R.id.my_order5:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), OrderManagementActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("logo", "0");
                    intent.putExtras(bundle);
                    startActivity(intent);

                }
                break;

            case R.id.relativeLayout1:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), MessageActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.relativeLayout2:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), ArticleCollectsActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.relativeLayout3:
                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), MyPrescriptionActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.relativeLayout4:
                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), MedicationDiaryActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.relativeLayout5:
                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), PersonalDataActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.relativeLayout6:
                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), SetUpActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.relativeLayout7:
                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), MyBalanceActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.relativeLayout8:
                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), AboutUsActivity.class);
                    startActivity(intent);
                }
                break;

        }

    }


    //获取用户资料
    public void getUserMessage() {

        //判断是否登录了
        String token = MySharedPreferences.getInstance(this.getActivity()).getString(MySharedPreferences.token, "");

        if (AllUtil.isNotNull(token)) {

            //已经登录
            login_registered.setText(MySharedPreferences.getInstance(this.getActivity()).getString(MySharedPreferences.name, ""));
            phone.setText(MySharedPreferences.getInstance(this.getActivity()).getString(MySharedPreferences.mobile, ""));
            Glide.with(getActivity()).load(MySharedPreferences.getInstance(this.getActivity()).getString(MySharedPreferences.avatar, "")).into(image_head);

        }


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_CANCELED) {
            return;
        }

        switch (requestCode) {

            case 10:

                Toast.makeText(this.getActivity(), "成功登录", Toast.LENGTH_LONG).show();
                getUserMessage();

                break;

            case 0:

                getUserMessage();

                break;

        }

    }


    @Override
    public void onResume() {
        super.onResume();

        if (!MyApplication.isLogin_state()) {

            login_registered.setText("注册/登录");
            phone.setText("");
            Glide.with(getActivity()).load("http://zmx.123xk.cn/static/upload/member/default.png").into(image_head);

        }else{
            getUserMessage();
        }

    }

}